// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/expression/vector_function.h>
#include <pollux/functions/lib/is_null.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>
#include <pollux/parse/type_resolver.h>

namespace kumo::pollux::functions {

void registerIsNotNull() {
  parse::registerTypeResolver();
  registerIsNotNullFunction("isnotnull");
}

} // namespace kumo::pollux::functions

using namespace kumo::pollux;
using namespace kumo::pollux::test;

class IsNotNullTest : public functions::test::FunctionBaseTest {
 public:
  static void SetUpTestCase() {
    functions::registerIsNotNull();
    memory::MemoryManager::testingSetInstance({});
  }

  template <typename T>
  std::optional<bool> isnotnull(std::optional<T> arg) {
    return evaluateOnce<bool>("isnotnull(c0)", arg);
  }

  SimpleVectorPtr<bool> isnotnull(const VectorPtr& arg) {
    return evaluate<SimpleVector<bool>>("isnotnull(c0)", make_row_vector({arg}));
  }
};

TEST_F(IsNotNullTest, singleValues) {
  EXPECT_FALSE(isnotnull<int32_t>(std::nullopt).value());
  EXPECT_FALSE(isnotnull<double>(std::nullopt).value());
  EXPECT_TRUE(isnotnull<int64_t>(1).value());
  EXPECT_TRUE(isnotnull<float>(1.5).value());
  EXPECT_TRUE(isnotnull<std::string>("1").value());
  EXPECT_TRUE(isnotnull<double>(std::numeric_limits<double>::max()).value());
}

// The rest of the file is copied and modified from IsNullTest.cpp

TEST_F(IsNotNullTest, flatInput) {
  vector_size_t size = 20;

  // All nulls.
  auto allNulls = make_flat_vector<int32_t>(
      size, [](auto /*row*/) { return 0; }, null_every(1));
  auto result = isnotnull(allNulls);
  assertEqualVectors(make_constant(false, size), result);

  // Nulls in odd positions: 0, null, 2, null,..
  auto oddNulls = make_flat_vector<int32_t>(
      size, [](auto row) { return row; }, null_every(2, 1));
  result = isnotnull(oddNulls);
  for (int i = 0; i < size; ++i) {
    EXPECT_EQ(result->value_at(i), i % 2 == 0) << "at " << i;
  }

  // No nulls.
  auto noNulls = make_flat_vector<int32_t>(size, [](auto row) { return row; });
  result = isnotnull(noNulls);
  assertEqualVectors(make_constant(true, size), result);
}

TEST_F(IsNotNullTest, constantInput) {
  vector_size_t size = 1'000;

  // Non-null constant.
  auto data = make_constant<int32_t>(75, size);
  auto result = isnotnull(data);

  assertEqualVectors(make_constant<bool>(true, size), result);

  // Null constant.
  data = make_constant<int32_t>(std::nullopt, size);
  result = isnotnull(data);

  assertEqualVectors(make_constant<bool>(false, 1'000), result);
}

TEST_F(IsNotNullTest, dictionary) {
  vector_size_t size = 1'000;

  // Dictionary over flat, no nulls.
  auto flatNoNulls = make_flat_vector<int32_t>({1, 2, 3, 4});
  auto dict = wrap_in_dictionary(
      make_indices(size, [](auto row) { return row % 4; }), size, flatNoNulls);

  auto result = isnotnull(dict);

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), !dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over no-nulls flat vector.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return row % 4; }),
      size,
      flatNoNulls);

  result = isnotnull(dict);

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), !dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary over flat vector with nulls.
  auto flatWithNulls = make_nullable_flat_vector<int32_t>({1, 2, std::nullopt, 4});
  dict = wrap_in_dictionary(
      make_indices(size, [](auto row) { return row % 4; }), size, flatWithNulls);

  result = isnotnull(dict);

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), !dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over flat vector with nulls.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return row % 4; }),
      size,
      flatWithNulls);

  result = isnotnull(dict);

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), !dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over constant.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return 2; }),
      size,
      make_constant<int32_t>(75, 10));

  result = isnotnull(dict);

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), !dict->is_null_at(i)) << "at " << i;
  }
}
